﻿package mortal.game.view.task.guild
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.proxy.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.view.common.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.task.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuildTaskGetWinNew extends SpecialWindow
    {
        private var _bmpBg:GImageBitmap;
        private var _items:Array;
        private var _txtTaskNum:GTextFiled;
        private var _leftSeconds:int = 0;
        private var _btnGet:GLoadingButton;
        private var _btnOneKeyOrange:GLoadingButton;
        private var _btnOneKeyShare:GLoadingButton;
        private var _txtCurColor:GTextFiled;
        private var _txtNeed:GTextFiled;
        private var _taskInfo:TaskInfo;
        private var _lastColor:int = -1;
        private var _cords:Array;
        private var _lastClickTime:int = -1;
        public static var isDoubleRewards:Boolean = false;
        public static var isShow:Boolean = false;
        public static var needClickShare:Boolean = false;

        public function GuildTaskGetWinNew(param1:ILayer = null)
        {
            this._cords = [130, 489, 71, 282, 328, 172, 596, 281, 515, 490];
            this.layer = LayerManager.windowLayer;
            this.setSize(764, 592);
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            isShow = true;
            GuideAutoDoTask.isNotAutoDoTask = true;
            return;
        }// end function

        public function updateTaskColor(param1:int) : void
        {
            if (this._taskInfo == null)
            {
                return;
            }
            this.updateTask(this._taskInfo, param1);
            return;
        }// end function

        override public function stageResize() : void
        {
            this.x = (Global.stage.stageWidth - 764) / 2;
            this.y = (Global.stage.stageHeight - 592) / 2;
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            isShow = false;
            GuideAutoDoTask.isNotAutoDoTask = false;
            if (!AIManager.isWorking)
            {
                if (this._taskInfo != null && this._taskInfo.isDoing())
                {
                    TaskUtil.linkTask(this._taskInfo);
                }
            }
            return;
        }// end function

        public function setInfos(param1:TaskInfo, param2:int) : void
        {
            this._taskInfo = param1;
            this.updateTask(this._taskInfo, param2);
            this.updateTodayTimes();
            if (Cache.instance.npc.selectedNpc == null)
            {
                EffectManager.glowFilterReg(this._btnGet, null, 0.2, 4, 0.1);
            }
            else
            {
                EffectManager.glowFilterUnReg(this._btnGet);
            }
            return;
        }// end function

        public function updateTodayTimes() : void
        {
            var _loc_1:* = Cache.instance.task.getGroupFinishCount(ETaskGroup._ETaskGroupGuild);
            var _loc_2:String = "#FCE700";
            this._btnGet.filters = [];
            if (_loc_1 >= 10)
            {
                _loc_2 = "#ff0000";
                this._btnGet.filters = [FilterConst.colorFilter];
                this._btnGet.mouseEnabled = false;
                this._btnGet.mouseChildren = false;
            }
            this._txtTaskNum.htmlText = "<font color=\'" + _loc_2 + "\'>" + _loc_1 + "/10</font>";
            return;
        }// end function

        private function updateTask(param1:TaskInfo, param2:int) : void
        {
            var _loc_7:GuildTaskItem = null;
            var _loc_8:Array = null;
            var _loc_9:Point = null;
            if (isDisposed)
            {
                return;
            }
            var _loc_3:* = param2;
            if (param2 < 0)
            {
                param2 = 0;
            }
            if (param1 == null)
            {
                this._btnGet.styleName = ImagesConst.GuildTaskGetBtn_upSkin;
                this.showOnlyGetMode();
                return;
            }
            var _loc_4:* = this.getBaseReward();
            var _loc_5:int = 0;
            while (_loc_5 < this._items.length)
            {
                
                _loc_7 = this._items[_loc_5];
                _loc_8 = TaskConfig.instance.getGuildTaskRewardTimes(_loc_5);
                _loc_7.updateTask(_loc_5, _loc_4[0] * _loc_8[0], _loc_4[1] * _loc_8[1]);
                if (_loc_5 == param2)
                {
                    _loc_7.setSelected(true);
                    if (param2 != this._lastColor && this._lastColor != -1 && !this._taskInfo.isComplete())
                    {
                        _loc_9 = this._btnGet.localToGlobal(GameConst.yellowPoint);
                        _loc_9.x = _loc_9.x + 30;
                        _loc_9.y = _loc_9.y + 10;
                        _loc_7.flyToPoint(_loc_9);
                    }
                }
                else
                {
                    _loc_7.setSelected(false);
                }
                _loc_5++;
            }
            if (param1.isComplete())
            {
                this._btnGet.styleName = ImagesConst.GuildTaskFinishBtn_upSkin;
                this.showOnlyGetMode();
            }
            else
            {
                if (param1.isCanget())
                {
                    this._btnGet.styleName = ImagesConst.GuildTaskGetBtn_upSkin;
                }
                else if (param1.isDoing())
                {
                    this._btnGet.styleName = ImagesConst.GuildTaskDoubleForwardBtn_upSkin;
                }
                if (Cache.instance.npc.selectedNpc == null)
                {
                    this.showOnlyGetMode();
                }
                else if (this._taskInfo.playerTask != null && this._taskInfo.playerTask.canShare || this._taskInfo.isCanget() && param2 == EColor._EColorOrange)
                {
                    if (this._taskInfo.isCanget() && param2 == EColor._EColorOrange)
                    {
                        needClickShare = true;
                    }
                    this.showShareMode();
                }
                else
                {
                    this.showOneKeyOrangeMode();
                }
            }
            this._lastColor = _loc_3;
            var _loc_6:* = ColorConfig.instance.getItemColor(param2);
            if (_loc_6 != null)
            {
                this._txtCurColor.textColor = _loc_6.intColor;
                this._txtCurColor.text = Language.getStringByParam(21171, _loc_6.colorText);
            }
            return;
        }// end function

        private function showOnlyGetMode() : void
        {
            DisplayUtil.removeMe(this._txtNeed);
            DisplayUtil.removeMe(this._txtCurColor);
            EffectManager.glowFilterUnReg(this._btnOneKeyShare);
            DisplayUtil.removeMe(this._btnOneKeyOrange);
            DisplayUtil.removeMe(this._btnOneKeyShare);
            this._btnGet.x = 331;
            return;
        }// end function

        private function showShareMode() : void
        {
            DisplayUtil.removeMe(this._btnOneKeyOrange);
            DisplayUtil.removeMe(this._txtNeed);
            this.addChild(this._txtCurColor);
            this.addChild(this._btnOneKeyShare);
            if (needClickShare)
            {
                EffectManager.glowFilterReg(this._btnOneKeyShare);
            }
            else
            {
                EffectManager.glowFilterUnReg(this._btnOneKeyShare);
            }
            this._btnGet.x = 265;
            this._btnOneKeyShare.x = this._btnGet.x + 145;
            return;
        }// end function

        private function showOneKeyOrangeMode() : void
        {
            EffectManager.glowFilterUnReg(this._btnOneKeyShare);
            DisplayUtil.removeMe(this._btnOneKeyShare);
            this.addChild(this._btnOneKeyOrange);
            this.addChild(this._txtCurColor);
            this.addChild(this._txtNeed);
            this._btnGet.x = 265;
            this._btnOneKeyOrange.x = this._btnGet.x + 145;
            return;
        }// end function

        public function getBaseReward() : Array
        {
            var _loc_4:Array = null;
            var _loc_1:Array = [];
            var _loc_2:int = 1;
            var _loc_3:* = TaskConfig.instance.getGuildTaskBaseReward(Cache.instance.role.roleInfo.level);
            if (_loc_3 == null || _loc_3.valueStr == null || _loc_3.valueStr.length == 0)
            {
                _loc_1.push(20302 * _loc_2);
                _loc_1.push(67 * _loc_2);
            }
            else
            {
                _loc_4 = _loc_3.valueStr.split(",");
                _loc_1.push(parseInt(_loc_4[0]) * _loc_2);
                _loc_1.push(parseInt(_loc_4[1]) * _loc_2);
            }
            return _loc_1;
        }// end function

        override protected function updateBtnSize() : void
        {
            if (_closeBtn)
            {
                _closeBtn.x = 775 - _closeBtn.width - 20;
                _closeBtn.y = 10;
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            var _loc_3:GuildTaskItem = null;
            super.createDisposedChildrenImpl();
            this.setSize(764, 592);
            _titleSpriteHight = 100;
            this._bmpBg = UIFactory.gImageBitmap(null, 10, 32, this);
            LoaderHelp.setBitmapdata(ImagesConst.GuildTaskWinBg + ".swf", this._bmpBg);
            LoaderHelp.addResCallBack(ResFileConst.taskGuild, this.resGotHandler);
            this._items = [];
            var _loc_1:int = 0;
            while (_loc_1 < 5)
            {
                
                _loc_3 = UICompomentPool.getUICompoment(GuildTaskItem) as GuildTaskItem;
                _loc_3.pzPre = "GuildTask_PZ";
                this.addChild(_loc_3);
                _loc_3.x = this._cords[_loc_1 * 2];
                _loc_3.y = this._cords[_loc_1 * 2 + 1] - 40;
                this._items.push(_loc_3);
                _loc_1++;
            }
            var _loc_2:* = GlobalStyle.textFormatPutong;
            _loc_2.size = 18;
            _loc_2.color = 16574208;
            this._txtTaskNum = UIFactory.gTextField("", 382, 456, 120, 26, this, _loc_2);
            this._txtTaskNum.filters = [FilterConst.NpcNameFilter];
            _loc_2 = GlobalStyle.textFormatPutong;
            _loc_2.color = 15850817;
            _loc_2.align = TextFormatAlign.CENTER;
            this._btnGet = UIFactory.gLoadingButton("", 265, 510, 118, 44, this);
            this._btnOneKeyShare = UIFactory.gLoadingButton(ImagesConst.GuildTaskShareOrangeBtn_upSkin, this._btnGet.x + 145, 510, 118, 44, this);
            this._btnOneKeyOrange = UIFactory.gLoadingButton(ImagesConst.GuildTaskOnKeyOrangeBtn_upSkin, this._btnOneKeyShare.x, this._btnOneKeyShare.y, 118, 44);
            this._btnGet.configEventListener(MouseEvent.CLICK, this.clickBtnHandler);
            this._btnOneKeyShare.configEventListener(MouseEvent.CLICK, this.oneKeyShareHandler);
            this._btnOneKeyOrange.configEventListener(MouseEvent.CLICK, this.oneKeyOrangeHandler);
            _loc_2 = GlobalStyle.textFormatLv;
            this._txtCurColor = UIFactory.gTextField("", this._btnGet.x + 18, this._btnGet.y + 45, 120, 20, this, _loc_2.clone());
            this._txtNeed = UIFactory.gTextField("", this._btnOneKeyOrange.x + 21, this._btnOneKeyOrange.y + 45, 120, 20, this, _loc_2.clone());
            this._txtNeed.htmlText = Language.getString(21172);
            return;
        }// end function

        private function oneKeyShareHandler(param1) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.TaskGuildShareSuccess, this.shareSucessHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskGuildShareSuccess, this.shareSucessHandler);
            if (this._taskInfo != null && this._taskInfo.isCanget() && Cache.instance.npc.selectedNpc != null)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskAskNextStep, [this._taskInfo, Cache.instance.npc.selectedNpc.npcInfo]));
            }
            GameProxy.task.shareGuildTask();
            EffectManager.glowFilterUnReg(this._btnOneKeyShare);
            GameProxy.task.guildTaskGetShareList();
            return;
        }// end function

        private function shareSucessHandler(param1) : void
        {
            needClickShare = false;
            this.hide();
            return;
        }// end function

        private function oneKeyOrangeHandler(param1) : void
        {
            var oneKeyclose:Function;
            var evt:* = param1;
            oneKeyclose = function (param1:int) : void
            {
                var _loc_2:NPCInfo = null;
                if (param1 == Alert.OK)
                {
                    if (_taskInfo.isCanget())
                    {
                        if (Cache.instance.npc.selectedNpc != null)
                        {
                            _loc_2 = Cache.instance.npc.selectedNpc.npcInfo;
                        }
                        GameController.guide.ismustOrange = true;
                        Dispatcher.dispatchEvent(new DataEvent(EventName.TaskAskNextStep, [_taskInfo, _loc_2]));
                    }
                    GameProxy.task.guildTaskOneKeyOrange();
                }
                return;
            }// end function
            ;
            if (Cache.instance.role.money.gold >= 10)
            {
                Alert.show(Language.getString(21173), null, Alert.OK | Alert.CANCEL, null, oneKeyclose);
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getString(21174));
            }
            return;
        }// end function

        private function clickBtnHandler(event:MouseEvent) : void
        {
            var _loc_4:NPCInfo = null;
            var _loc_2:* = getTimer();
            if (_loc_2 - this._lastClickTime < 500)
            {
                return;
            }
            if (Cache.instance.role.roleInfo.level < 45)
            {
                MsgManager.showRollTipsMsg(Language.getString(21175));
                return;
            }
            var _loc_3:* = Cache.instance.task.getTaskInfoByGroup(ETaskGroup._ETaskGroupGuild);
            if (this._taskInfo == null || _loc_3 == null)
            {
                MsgManager.showRollTipsMsg(Language.getString(21176));
                return;
            }
            if (this._taskInfo != null && !this._taskInfo.isCanget() && !this._taskInfo.isComplete())
            {
                MsgManager.showRollTipsMsg(Language.getString(21177));
                TaskUtil.linkTask(this._taskInfo);
                this.hide();
                return;
            }
            this._lastClickTime = _loc_2;
            TaskProxy.clickShareTime = -1;
            if (Cache.instance.npc.selectedNpc != null)
            {
                _loc_4 = Cache.instance.npc.selectedNpc.npcInfo;
            }
            if (_loc_4 == null)
            {
                this.hide();
                TaskUtil.linkTask(this._taskInfo);
            }
            else
            {
                this.hide();
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskAskNextStep, [this._taskInfo, _loc_4]));
            }
            return;
        }// end function

        private function resGotHandler() : void
        {
            if (isDisposed)
            {
                return;
            }
            this.titleBitmapData = GlobalClass.getBitmapData(ImagesConst.GuildTask_Title);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._lastColor = -1;
            NetDispatcher.removeCmdListener(ServerCommand.TaskGuildShareSuccess, this.shareSucessHandler);
            EffectManager.glowFilterUnReg(this._btnGet);
            EffectManager.glowFilterUnReg(this._btnOneKeyShare);
            this._bmpBg.dispose(param1);
            this._bmpBg = null;
            this._txtTaskNum.dispose(param1);
            this._txtTaskNum = null;
            DisplayUtil.delNotUse(this._items, 0);
            this._items = null;
            this._btnGet.dispose(param1);
            this._btnGet = null;
            this._txtCurColor.dispose(param1);
            this._txtCurColor = null;
            this._txtNeed.dispose(param1);
            this._txtNeed = null;
            return;
        }// end function

    }
}
